package org.datastructure.secondday.search;

public class BinarySearch {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 29};
        System.out.println(binarySearch(arr, 1, 0, arr.length - 1));

    }

    public static int binarySearch(int[] arr, int key, int left, int right) {
        if (left > right) return -1;

        int mid = (left + right) / 2;
        if (key < arr[mid]) {
            return binarySearch(arr, key, left, mid - 1);
        } else if (key > arr[mid]) {
            return binarySearch(arr, key, mid + 1, right);
        } else {
            return mid;
        }

    }
}
